package hadoop.mapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @describe: 统计文件中每个单词出现的次数
 */
public class WordCount {

    //2020-04-14 16:34:03.800 [main] INFO
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
        System.setProperty("HADOOP_USER_NAME","root");
        String HDFS_URI = "hdfs://hadoop1:9000/";
        String USER = "root";
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        conf.set("dfs.client.use.datanode.hostname", "true");
        conf.set("dfs.replication", "1");
        FileSystem fs = FileSystem.get(new URI(HDFS_URI), conf, USER);
        Job job = Job.getInstance(conf);

        //job所在的jar包
        job.setJarByClass(WordCount.class);
        //job使用的mapper类 Reducer类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        //job作业mapper 的输出 key， value
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //job 作业reducer 的输出 key value
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //设置reduce 的个数
        job.setNumReduceTasks(1);
        String inputPath = "/user/root/input";
        String outputPath = "/user/root/output";
        //判断output文件夹是否存在存在则删除
        Path path = new Path(outputPath);
        FileSystem fileSystem = path.getFileSystem(conf);
        if(fileSystem.exists(path)){
            fileSystem.delete(path, true);
        }
        //设置本次job的输入 输出
        FileInputFormat.setInputPaths(job, new Path(inputPath));
        FileOutputFormat.setOutputPath(job, new Path(outputPath));

        //提交job
        job.waitForCompletion(true);
    }

    static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] strArray = line.split(" ");
            Text domain = new Text(strArray[1]);
            context.write(new Text(domain), new LongWritable(1));
        }
    }

    static class WordCountReducer extends Reducer<Text, LongWritable, Text,LongWritable> {
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long counter = 0;
            for(LongWritable value : values){
                counter += value.get();
            }
            context.write(key, new LongWritable(counter));
        }
    }
}
